use std::str::FromStr;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config {
    pub level: String,
}

pub struct Setting {
    pub level: tracing::Level,
}

impl TryFrom<&Config> for Setting {
    type Error = anyhow::Error;
    fn try_from(value: &Config) -> Result<Self, Self::Error> {
        let level = tracing::Level::from_str(value.level.as_str()).map_err(|e| anyhow::Error::new(e))?;

        Ok(Self {
            level,
        })
    }
}

pub fn init(setting: Setting) {
    tracing_subscriber::fmt()
        .with_max_level(setting.level)
        .with_timer(tracing_subscriber::fmt::time::LocalTime::rfc_3339())
        .with_target(true)
        .with_file(true)
        .with_line_number(true)
        .init();
}